package cn.edu.jnu.x2022103548

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var game: Game
    private lateinit var rollInput: EditText
    private lateinit var scoreText: TextView

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main) // 使用XML布局

        // 初始化组件
        game = Game()
        rollInput = findViewById(R.id.rollInput)
        scoreText = findViewById(R.id.scoreText)
        val rollButton = findViewById<Button>(R.id.rollButton)

        // 按钮点击事件
        rollButton.setOnClickListener {
            try {
                val pins = rollInput.text.toString().toInt()
                game.roll(pins)
                scoreText.text = getString(R.string.score_text, game.score())
                //scoreText.setText(getString(R.string.score_format, game.score()));

                rollInput.text.clear()
            } catch (e: NumberFormatException) {
                Toast.makeText(this, "请输入有效数字", Toast.LENGTH_SHORT).show()
            }
        }
    }
}